#!/bin/sh


STARTTIME=$(date +%s)

duration () {
    expr $(date +%s) - $STARTTIME
}


GOT_JACKD=0
GOT_JACKDMP=0

osascript -e 'display dialog "Please wait, killing jack for 10 seconds" buttons {"OK"}' &
PIDOFF_OSA=$!

sleep 1
killall -9 JackPilot
killall -9 qjackctl
killall -9 QJackCtl

# Try for 10 seconds.
while [ $(duration) -lt 10 ] ; do 

    if ! killall -9 jackd ; then
        
        echo "killall -9 jackd"
    
        #echo "finished killing jackd" (commented out. can't trust the output of killall)
        #GOT_JACKD=1
    else
        GOT_JACKD=0 # maybe it reappared?
    fi
    
    if ! killall -9 jackdmp ; then
        echo "killall -9 jackdmp"
        #echo "finished killing jackdmp" (commented out. can't trust the output of killall)
        #GOT_JACKDMP=1
    else
        GOT_JACKDMP=0 # maybe it reappared?
    fi

    if [ $GOT_JACKD -eq 1 ] ; then
        if [ $GOT_JACKDMP -eq 1 ] ; then
            exit 0
        fi
    fi

#    echo $(duration)
    sleep 0.01
done

kill $PIDOFF_OSA

osascript -e 'display dialog "Jack should be dead now. Now you can restart Jack and your applications." buttons {"OK"}'

#exit 1  (commented out. can't trust the output of killall)
